//+------------------------------------------------------------------+ //| [ea]SimpleTMTv0.2.mq4 | //| Bart Pekala | //| | //+------------------------------------------------------------------+ #property copyright "Bart Pekala" #property link "bart.pekala@gmail.com" #include extern string UseGlobalString0 ="// UseGlobalVars:"; extern string UseGlobalString1 ="// force using Global Variables(F3).."; extern string UseGlobalString2 ="// ..to pass time, SL and TP values"; extern bool UseGlobalVars =false; extern string WhenToTrade ="12:00"; extern string TypeString0 ="// TypeOfOrder:"; extern string TypeString1 ="// 0 = market buy; 1 = market sell"; extern string TypeString2 ="// 2 = buy limit; 3 = sell limit"; extern string TypeString3 ="// 4 = buy stop; 5 = sell stop"; extern int TypeOfOrder =0; extern double Lots =0.1; extern int MaxSlippagePips =3; extern int MaxDelayMinutes =3; extern string PriceString ="// entry price (pending orders only):"; extern double EntryPrice =0.0; extern int SLDistancePips =100; extern int TPDistancePips =100; extern string virString1 ="// VirginityFilter: don\'t send order..."; extern string virString2 ="// if the level\'s been breached (pending only)"; extern bool UseVirginityFilter=false; extern int EAMagicNumber =111009; datetime tradingTime =0; int orderSendDay =0; bool needToModify =false; double maxSinceInit =0; double minSinceInit =0; int dayOfMonth; int minsToNextTrade; datetime now; int ticket; bool result; string chartSymbol; double entry; double sl; double tp; double oop; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- if(MaxDelayMinutes < 1) MaxDelayMinutes= 1; //it has to be at least 1 chartSymbol =Symbol(); start(); //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- Comment(""); //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- if(UseGlobalVars) ReadGlobalVars(); now =TimeCurrent(); tradingTime =StrToTime(WhenToTrade); if(now > tradingTime+MaxDelayMinutes*60) tradingTime+=(PERIOD_D1*60); minsToNextTrade=((tradingTime-now)/60); Comment("SimpleTMT - time to next trade: ",minsToNextTrade," minutes"); dayOfMonth =Day(); UpdateMinMax(); if(now < tradingTime) {return(0);} //not yet... if(now > tradingTime+MaxDelayMinutes*60) {return(0);} //too late... if(UseVirginityFilter && TypeOfOrder > 1 && !IsAVirgin(EntryPrice)) { Print("level breached; not sending orders"); return(0); } if(orderSendDay != dayOfMonth) //didn't we already trade? { entry =GetEntryPrice(); //sending order... ticket =OrderSendCustom(chartSymbol,TypeOfOrder,Lots,entry, MaxSlippagePips,0,0,"",EAMagicNumber); if(ticket<0) return(0); else orderSendDay =dayOfMonth; //ticket is OK; no more trading today, now let's modify... needToModify =true; } if(needToModify) //only if OrderSend() was successful { if(!OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) //check if order exists { Print("can\'t modify order #",ticket,"; order not found"); needToModify =false; return(0); } SetSLTP(); //order sl, tp and open price result =OrderModifyCustom(ticket,oop,sl,tp,0); if(!result) return(0); else needToModify =false; } //---- return(0); } //+------------------------------------------------------------------+ bool IsAVirgin(double myPrice) { if(myPrice > maxSinceInit || myPrice < minSinceInit) return(true); else return(false); } int UpdateMinMax() { if(Bid > maxSinceInit) maxSinceInit =Bid; if(Bid < minSinceInit) minSinceInit =Bid; return; } double GetEntryPrice() { //order entry price if(TypeOfOrder == 0) return(Ask); else if(TypeOfOrder == 1) return(Bid); else return(NormalizeDouble(EntryPrice,Digits)); } void SetSLTP() { OrderSelect(ticket,SELECT_BY_TICKET); oop =OrderOpenPrice(); if(TypeOfOrder%2 == 0) //long { sl =NormalizeDouble(oop - SLDistancePips*Point,Digits); tp =NormalizeDouble(oop + TPDistancePips*Point,Digits); } else //short { sl =NormalizeDouble(oop + SLDistancePips*Point,Digits); tp =NormalizeDouble(oop - TPDistancePips*Point,Digits); } } bool ReadGlobalVars() { double hourGlobal; double minutesGlobal; //read global hour if(GlobalVariableCheck("SimpleTMT_WhenToTrade_Hour")) hourGlobal =GlobalVariableGet("SimpleTMT_WhenToTrade_Hour"); else GlobalVariableSet("SimpleTMT_WhenToTrade_Hour",12); //read global minutes if(GlobalVariableCheck("SimpleTMT_WhenToTrade_Minutes")) minutesGlobal =GlobalVariableGet("SimpleTMT_WhenToTrade_Minutes"); else GlobalVariableSet("SimpleTMT_WhenToTrade_Minutes",0); WhenToTrade=StringConcatenate(DoubleToStr(hourGlobal,0),":",DoubleToStr(minutesGlobal,0)); //read global SL if(GlobalVariableCheck("SimpleTMT_SL_Distance")) SLDistancePips =GlobalVariableGet("SimpleTMT_SL_Distance"); else GlobalVariableSet("SimpleTMT_SL_Distance",100); //read global TP if(GlobalVariableCheck("SimpleTMT_TP_Distance")) TPDistancePips =GlobalVariableGet("SimpleTMT_TP_Distance"); else GlobalVariableSet("SimpleTMT_TP_Distance",100); return(true); } bool OrderModifyCustom( int ticket, double price, double stoploss, double takeprofit, datetime expiration, color arrow_color=CLR_NONE) { bool result; GetLastError(); //emptying error buffer while(IsTradeContextBusy()) Sleep(200); while(true) { RefreshRates(); result= OrderModify(ticket,price,stoploss,takeprofit,expiration,arrow_color); if(result) return(true); if(!result) { int errorCode= GetLastError(); if(errorCode==146) //try again if Trade Context busy { Sleep(200); continue; } else if(errorCode>1) { Alert(":( error while modifying order: ",ErrorDescription(errorCode)); return(false); } } } return(false); } int OrderSendCustom( string mySymbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment="", int magic=0, datetime expiration=0, color arrow_color=CLR_NONE) { GetLastError(); //emptying error buffer while(IsTradeContextBusy()) Sleep(200); while(true) { RefreshRates(); int ticket= OrderSend(mySymbol,cmd,volume,price,slippage,stoploss, takeprofit,comment,magic,expiration,arrow_color); if(ticket == -1) { int errorCode= GetLastError(); if(errorCode==146) { Sleep(200); continue; } else if(errorCode!=0) { Alert(":( error while sending order: ",ErrorDescription(GetLastError())); break; } } else return(ticket); } }